﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PGMHelper;
using System.Windows.Forms.DataVisualization.Charting;

namespace GBCodeSpecturmDemo
{
    /// <summary>
    /// 反应谱集合
    /// </summary>
    public class GBCodeSpectrumSet
    {
        /// <summary>
        /// 反应谱列表
        /// </summary>
        private List<GBCodeSpectrum> spectrumList { set; get; }

        /// <summary>
        /// 反应谱列表
        /// </summary>
        public List<GBCodeSpectrum> SpectrumList
        {
            get
            {
                return (from spectrum in this.spectrumList
                        select spectrum).ToList();
            }
        }

        /// <summary>
        /// 数据点数目
        /// </summary>
        public int Count
        {
            get
            {
                return this.spectrumList.Count;
            }
        }

        /// <summary>
        /// 周期长度
        /// </summary>
        private int PeriodLength { set; get; }

        /// <summary>
        /// 周期增量
        /// </summary>
        public double DeltaPeriod { set; get; }

        /// <summary>
        /// 6s后是否拉平处理
        /// </summary>
        private bool is6Horizontal { set; get; }

        /// <summary>
        /// 获得数据
        /// </summary>
        /// <returns></returns>
        public GridViewDatas GetData(List<GBCodeSpectrum> targertSpectrums)
        {
            //初始化
            var data = new GridViewDatas("反应谱数据");
            //没有数据直接返回
            if (targertSpectrums.Count == 0) return data;
            //周期数据
            data.Add(new GridViewColumn(this.spectrumList.First().GetValueList("Period"), titleName: "周期",
                demical: 2, isreadOnly: true));
            //反应谱数据
            targertSpectrums.ForEach(specturm =>
             data.Add(new GridViewColumn(specturm.GetValueList("Afa"), titleName: specturm.Descp,
                demical: 5, isreadOnly: true)));
            //返回数据
            return data;
        }

        /// <summary>
        /// 绘图
        /// </summary>
        /// <param name="chart"></param>
        public void ToChart(Chart chart, List<GBCodeSpectrum> targertSpectrums)
        {
            //数据清空
            chart.Series.Clear();
            //反应谱绘制
            targertSpectrums.ForEach(spectrum => spectrum.ToChart(chart));
            //设定最大值
            chart.SetAxisBroundary(ChartAxisType.AxisX, 0, this.PeriodLength, this.PeriodLength, 0);
        }

        /// <summary>
        /// 添加反应谱
        /// </summary>
        /// <param name="textBoxList"></param>
        /// <returns></returns>
        public bool Add(List<TextBox> textBoxList)
        {
            try
            {
                //获得参数
                var afa = Convert.ToDouble(textBoxList[0].Text);
                var tg = Convert.ToDouble(textBoxList[1].Text);
                var damp = Convert.ToDouble(textBoxList[2].Text);
                //增加反应谱
                this.spectrumList.Add(new GBCodeSpectrum(tg, afa, damp, 
                    this.is6Horizontal, this.PeriodLength, this.DeltaPeriod));
                //成功
                return true;
            }
            catch(Exception ex)
            {
                return MessageBoxExtern.Error(ex.Message);
            }
        }

        /// <summary>
        /// 清空
        /// </summary>
        public void Clear()
        {
            this.spectrumList.Clear();
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public GBCodeSpectrumSet()
        {
            //初始化
            this.spectrumList = new List<GBCodeSpectrum>();
            //默认值
            this.PeriodLength = 10;
            this.DeltaPeriod = 0.01;
            this.is6Horizontal = false;
        }
    }
}
